// https://leetcode.cn/problems/house-robber-ii/
// Created by ade on 2022/9/14.
//
#include <iostream>
#include <vector>

using namespace std;

class Solution {
public:
    int rob(vector<int> &nums) {
        int len = nums.size();
        if (len == 1) return nums[0];
        if (len == 2) return max(nums[0], nums[1]);
        return max(robRange(nums, 1, len), robRange(nums, 0, len - 1));
    }

    int robRange(vector<int> &nums, int start, int end) {
        int len = nums.size();
        vector<int> dp(len, 0);
        dp[start] = nums[start];
        dp[start + 1] = max(nums[start], nums[start + 1]);
        for (int i = start + 2; i < end; i++) {
            dp[i] = max(dp[i - 2] + nums[i], dp[i - 1]);
        }
        return dp[end - 1];
    }
};

int main() {
    Solution so;
    vector<int> nums = {1, 2, 1, 1};
    cout << so.rob(nums);
    return 0;
}